home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Financial / RISK / Source / RanGen.m < prev    next >
Text File  |  1995-06-12  |  1KB  |  52 lines

  1. #import "RanGen.h"
  2. #import <appkit/appkit.h>
  3. #import <math.h>
  4. #import <stdio.h>
  5. #import <sys/time.h>
  6.  
  7. @implementation RanGen
  8.  
  9.  
  10. // ----------------------------------------------------------------
  11. + new
  12. {
  13.   long t;
  14.   int seed;
  15.   self = [super new];  
  16.   time (&t);
  17.   seed = t; // is time-based!
  18. //  printf("RanGen: new random seed = %d\n",t);
  19.   srandom(seed);
  20.   return self;
  21. }
  22.  
  23.  
  24. // ----------------------------------------------------------------
  25. // ran01: Linear congruential method based on
  26. // the random() supplied by the C library.
  27. // Allegedly random() is better than rand().
  28. - (double) ran01 
  29. {
  30.   double aRand;
  31.   aRand = (double)random()/2147483648.0;
  32.   // printf("%lf\n",aRand);
  33.   return aRand;  
  34. } // end of ran01
  35.  
  36.  
  37. // ----------------------------------------------------------------
  38. - (int) lowInt: (int) lowestNumber  highInt: (int) highestNumber
  39. {
  40.   double r,t;
  41.   // integer arithmetic requires to add 1 to difference. If this
  42.   // is not done, the highest number is not included.
  43.   // for instance low=10, high=20. Then it has to be r=11, because
  44.   // random is never exactly equal to 1.
  45.   r = (double) highestNumber - (double) lowestNumber + 1;
  46.   t = r * [self ran01];
  47.   return lowestNumber + (int)t;
  48. }
  49.  
  50.  
  51. @end
  52.